package com.zzz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zzz.myexampleapp.R;
import com.zzz.mylibrary.util.LogZ;
import com.zzz.mylibrary.util.SpMMKV;
import com.zzz.mylibrary.util.ZToast;
import com.zzz.proxy.ICommonCallback;
import com.zzz.proxy.ProxyInvocation;
import com.zzz.proxy.ProxyInvocationT;
import com.zzz.proxy.ProxyUtil;
import java.lang.reflect.Proxy;
import org.luaj.vm2.Globals;
import org.luaj.vm2.lib.jse.JsePlatform;


/*
 * 编译前确定编译的是app还是mykotlinapp
 * */
public class MainActivity extends AppCompatActivity {
    //ButterKnife
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                LogZ.e("logg");
            }
        });
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, SecActivity.class));
                ZToast.makeText(MainActivity.this, "item2");
            }
        });
        btn3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String mmkvkey="mmkv0";
                ZToast.makeText(MainActivity.this, SpMMKV.getValue(mmkvkey));
            }
        });
        btn4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ZToast.makeText(MainActivity.this, "jump flutter");
                //startActivity(new Intent(MainActivity.this, com.example.easydoctor_flutter.));
            }
        });
        /*textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               // Intent intent=new Intent(MainActivity.this, Fanc)
            }
        });*/
    }

    @Override
    protected void onResume() {
        super.onResume();

        //测试proxy
        ICommonCallback proxyUtil = new ProxyUtil();
        ProxyInvocation proxyInvocation = new ProxyInvocation(proxyUtil);
        ICommonCallback commonCallback = (ICommonCallback) Proxy.newProxyInstance(
                proxyUtil.getClass().getClassLoader(), proxyUtil.getClass().getInterfaces(), proxyInvocation);
        commonCallback.proxyable();

        //正常使用
        ProxyUtil util=new ProxyUtil();
        util.proxyable();
        
        //代理方式
        ICommonCallback proxyUtil2 = new ProxyUtil();
        ProxyInvocationT proxyInvocation2 = new ProxyInvocationT<>(proxyUtil2);
        ICommonCallback commonCallback2 = (ICommonCallback) Proxy.newProxyInstance(
                proxyUtil2.getClass().getClassLoader(), new Class<?>[]{ICommonCallback.class}, proxyInvocation2);
        commonCallback2.proxyable();

        //读取lua脚本
        try {
            String script="hello.lua";
            Globals globals = JsePlatform.standardGlobals();
            globals.loadfile("hello.lua").call();
        }catch (Exception e){
            LogZ.e("error lua");
            e.printStackTrace();
        }
    
        //MMKV框架
        String mmkvkey="mmkv0";
        String str="hello mmkv";
        SpMMKV.putValue(mmkvkey,str);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                LogZ.e("logg");
                break;
            case R.id.item2:
                startActivity(new Intent(MainActivity.this, SecActivity.class));
                ZToast.makeText(this, "item2");
                break;
            case R.id.item3:
                String mmkvkey="mmkv0";
                ZToast.makeText(this, SpMMKV.getValue(mmkvkey));
                break;
            default:
        }

        return true;
    }
}
